﻿using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using StartEAS_API.Utils;
using StartsEAS_API.BLL.Interfaces.ITheApplicationFormB;
using StartsEAS_API.Model;
using StartsEAS_API.Model.Models;

namespace StartsEAS_API.Controllers.TheApplicationFormContor
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [EnableCors("any")]
    public class TheApplicationFormController : ControllerBase
    {
        private readonly ITheApplicationFormBLL _theApplicationFormBll;
        public TheApplicationFormController(ITheApplicationFormBLL theApplicationFormBll) { 
            _theApplicationFormBll = theApplicationFormBll;
        }
        /// <summary>
        /// 根据Token查询该用户的所有申请表
        /// </summary>
        /// <param name="token">用户token(学生、老师)</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllApplicationFormByToken(string token) {
            try
            {
                return _theApplicationFormBll.GetAllApplicationFormByToken(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllApplicationFormByToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 教务处查询所有的申请表
        /// </summary>
        /// <param name="token">教务处Token</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetAllApplicationForm(string token) {
            try
            {
                return _theApplicationFormBll.GetAllApplicationForm(token);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetAllApplicationForm服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 添加一条申请表信息
        /// </summary>
        /// <param name="token">用户token</param>
        /// <param name="InfoJSON">申请表具体信息</param>
        /// <param name="ApplicationType">申请类型</param>
        /// <param name="Status">当前状态</param>
        /// <returns></returns>
        [HttpPost]
        public ResultUtils InsertApplicationForm(string token, string InfoJSON, int ApplicationType, int Status) {
            try
            {
                System.Console.WriteLine("InfoJSON：" + InfoJSON);
                System.Console.WriteLine("Token：" + token);
                return _theApplicationFormBll.InsertApplicationForm(token, InfoJSON, ApplicationType, Status);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"InsertApplicationForm服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 老师审批
        /// </summary>
        /// <param name="token">老师token</param>
        /// <param name="status">1 同意 2 拒绝</param>
        /// <param name="applicationFormId">消息审批申请表Id</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils AlterStuCounselorApproval(string token, int status, int applicationFormId)
        {
            try
            {
                return _theApplicationFormBll.AlterStuCounselorApproval(token, status, applicationFormId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"AlterStuCounselorApproval服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 教务处审批
        /// </summary>
        /// <param name="token">教务处Token</param>
        /// <param name="status">1 同意 2 拒绝</param>
        /// <param name="theApplicationForm">消息审批申请表</param>
        /// <returns></returns>
        [HttpPut]
        public ResultUtils AlterStuDeptApproval(string token, int status, JObject theApplicationForm) {
            try
            {
                return _theApplicationFormBll.AlterStuDeptApproval(token, status, theApplicationForm);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"AlterStuDeptApproval服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
        /// <summary>
        /// 查询老师班上学生的信息修改申请表
        /// </summary>
        /// <param name="token">老师token</param>
        /// <param name="classId">班级Id</param>
        /// <returns></returns>
        [HttpGet]
        public ResultUtils GetStudentApplicationFormByWorkerToken(string token, string classId) {
            try
            {
                return _theApplicationFormBll.GetStudentApplicationFormByWorkerToken(token, classId);
            }
            catch (System.Exception e)
            {
                AppLog.WriteError($"GetStudentApplicationFormByWorkerToken服务器内部异常：{e}", true);
                return new ResultUtils() { code = 500, message = $"服务器内部异常\n{e.Message}" };
            }
        }
    }
}
